package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.annotation.PermInfo;
import com.wind.entity.Article;
import com.wind.entity.Comment;
import com.wind.service.IArticleService;
import com.wind.service.ICommentService;
import com.wind.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wind_com")
public class CommentController {
    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private ICommentService commentService;
    /*@PostMapping
    public Json add(@RequestBody String body) {

        String oper = "add atc";
        log.info("{}, body: {}",oper,body);

        Article atc = JSON.parseObject(body, Article.class);

        if (StringUtils.isEmpty(atc.getTitle())) {
            return Json.fail(oper, "文章标题不能为空");
        }
        if (StringUtils.isEmpty(atc.getImage())) {
            return Json.fail(oper, "图片不能为空");
        }
        if (StringUtils.isEmpty(atc.getLink())){
            return Json.fail(oper, "跳转链接不能为空");
        }
        Comment comDB = commentService.selectOne(new EntityWrapper<Comment>().eq("title", atc.getTitle()));
        if (comDB != null) {
            return Json.fail(oper, "文章已发布");
        }

        //保存新用户数据
        atc.setUpdateTime(new Date());
        atc.setCreateTime(new Date());

        boolean success = articleService.insert(atc);
        return Json.result(oper, success)
                .data("id",atc.getId())
                .data("createTime",atc.getCreateTime());
    }
*/
/*
    @PermInfo("删除文章")
    @DeleteMapping
    public Json delete(@RequestBody String body) {

        String oper = "delete atc";
        log.info("{}, body: {}",oper,body);
        Article atc = JSON.parseObject(body, Article.class);
        atc.setIsDelete(1);
        boolean success = articleService.updateById(atc);
        return Json.result(oper, success).data("isDelete",atc.getIsDelete());
    }
*/

    @PostMapping("/query")
    public Json query(@RequestBody String body) {
        String oper = "query com";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        String name = json.getString("name");
        Integer commentType = json.getInteger("commentType");
        Integer isActive = json.getInteger("isActive");
        Wrapper<Comment> wrapper=new EntityWrapper<>();
        wrapper.like("name",name);
        if(commentType!=null){
            wrapper.eq("comment_type",commentType);
        }
        if (isActive!=null){
            wrapper.eq("is_active",isActive);
        }
        Page<Comment> page = commentService.selectPage(new Page<>(1,10), wrapper);
        return Json.succ(oper).data("page", page);
    }


    @PermInfo("更新状态")
    @PatchMapping("/update")
    public Json update(@RequestBody String body) {

        String oper = "update com";
        log.info("{}, body: {}", oper, body);

        Comment com = JSON.parseObject(body, Comment.class);

        com.setUpdateTime(new Date());

        //boolean success = sysUserService.update(user,new EntityWrapper<User>().eq("uid",user.getUid()));
        boolean success = commentService.updateById(com);

        return Json.result(oper, success).data("isActive",com.getIsActive());
    }

    @PermInfo("批量更新")
    @PatchMapping("/batchUpdate")
    public Json batchUpdate(@RequestBody String body) {

        String oper = "batchUpdate com";
        log.info("{}, body: {}", oper, body);

        List<Comment> list = JSON.parseArray(body,Comment.class);
        boolean success = false;    
        for (Comment com:
             list) {
            com.setUpdateTime(new Date());
            success = commentService.updateById(com);
        }


        //boolean success = sysUserService.update(user,new EntityWrapper<User>().eq("uid",user.getUid()));


        return Json.result(oper, success).data("isActive",list.get(0).getIsActive());
    }

}
